package com.mayikt.pay.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mayikt.base.BaseResponse;
import com.mayikt.base.enums.ApiResponseEnum;
import com.mayikt.exception.BizException;
import com.mayikt.pay.mapper.entity.PaymentChannel;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import com.mayikt.base.BaseApiService;
import com.mayikt.pay.mapper.PaymentChannelMapper;
import com.mayikt.pay.service.PaymentChannelService;
import com.mayikt.weixin.out.dto.PaymentChannelDTO;

@RestController
@Service
public class PaymentChannelServiceImpl extends BaseApiService<JSONObject> implements PaymentChannelService {
	@Autowired
	private PaymentChannelMapper paymentChannelMapper;

	@Override
	@HystrixCommand(fallbackMethod = "hystrixFallback")
	public BaseResponse<JSONObject> selectAll() {
	    int x =10/0;
		QueryWrapper<PaymentChannel> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(PaymentChannel::getChannelState,1);
		List<PaymentChannel> paymentChanneList = paymentChannelMapper.selectList(queryWrapper);

		List<PaymentChannelDTO> paymentChannelDTOList = new ArrayList<>();
		for (PaymentChannel paymentChannel : paymentChanneList) {
			PaymentChannelDTO paymentChannelDTO = new PaymentChannelDTO();
			BeanUtils.copyProperties(paymentChannel,paymentChannelDTO);
			paymentChannelDTOList.add(paymentChannelDTO);
		}
        JSONObject data =(JSONObject) JSONObject.toJSON(paymentChannelDTOList);
        return setResultSuccess(data);
	}

	public BaseResponse<JSONObject> hystrixFallback(){
	    throw new BizException(ApiResponseEnum.INTERNAL_ERROR,"服务器正在火速处理请求，请喝杯茶休息一会");
	}
}
